#!/bin/bash
########################################################################
# (C) Copyright 2008 Muhammed Uluyol
########################################################################
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
########################################################################
#
# This script adds entries to the database

[ -f lib_helper.cfg ] || echo "CANT FIND CONFIG"
source lib_helper.cfg

[ -d $DB_DIR ] || mkdir -p $DB_DIR

clear
echo -e "\033[1mEnter the book title, DO NOT PRESS ENTER UNTIL DONE TYPING\033[0m"
read -p "TITLE: " title

clear
echo -e "\033[1mEnter the book author, DO NOT PRESS ENTER UNTIL DONE TYPING\033[0m"
read -p "AUTHOR: " author

clear
echo -e "\033[1mEnter the book summary as ONE LINE, DO NOT PRESS ENTER UNTIL DONE TYPING\033[0m"
read -p "SUMMARY: " summary

clear
echo -e "\033[1mEnter the book tags separated by commas, DO NOT USE SPACES, DO NOT PRESS ENTER UNTIL DONE TYPING\033[0m"
read -p "TAGS: " tags

clear
echo -e "\033[1m************************INFORMATION************************\033[0m"
echo -e "\033[1mTitle:\033[0m   $title"
echo -e "\033[1mAuthor:\033[0m  $author"
echo -e "\033[1mSummary:\033[0m $summary"
echo -e "\033[1mTags:\033[0m    $tags"
echo -e "\033[1m***********************************************************\033[0m"
echo -n "Press ENTER to continue..."
read junk
OLD_BOOK_NUM=$(ls -1 $DB_DIR | grep $DB_NAME | grep \.bk | sed -e s:$DB_NAME::g -e s:\.bk:: | sort -n | tail -1)
BOOK_NUM=$(echo $OLD_BOOK_NUM | awk '{ print $1 + 1 }')
FILE_TO_WRITE="${DB_NAME}${BOOK_NUM}.bk"
echo -n "Writing $FILE_TO_WRITE..."
cat << EOF >$DB_DIR/$FILE_TO_WRITE
TITLE: $title
AUTHOR: $author
SUMMARY: $summary
TAGS: $tags
EOF
echo "Done"
exit
